package cn.stylefeng.guns.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * TODO(这里用一句话描述这个类的作用)
 *
 * @author sq.xiao@lovego.com
 * @projectName lovego-micro-new
 * @package com.fairy.safety.core.util
 * @className DateTimeUtil
 * @date 2019/10/4 19:10
 */
public class DateTimeUtil {
	

	   /** 
	    * 获取过去第几天的日期 
	    * 
	    * @param past 
	    * @return 
	    */ 
	 public static String getPastDateRfc(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");  
	       String result = format.format(today);  
	       return result;  
	   }  
	 //时间戳为UTC时间. 时间戳JSON表示形式为RFC 3339格式
	 public static String getDateRfc(Date date){
	        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
	                .format(date);
	    }

    public static String getDateRfc(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
                .format(date);
    }

    public static String parseDateRfc(String dateStr){
    	//String tempTime = dateStr.replace("Z", " UTC");
    	//yyyy-MM-dd'T'HH:mm:ss.SSS Z"   yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(date);
    }
    public static String parseDateRfcEs(String dateStr){
    	//String tempTime = dateStr.replace("Z", " UTC");
    	//yyyy-MM-dd'T'HH:mm:ss.SSS Z"   yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(date);
    }

    public static String formatDateStr(Date date,String format){
        return new SimpleDateFormat(format)
                .format(date);
    }

    public static Date getDate(String dateStr,String formart){
        SimpleDateFormat sdf = new SimpleDateFormat(formart);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
           // System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
    /**
     * 获取下一个小时时间
     * @return
     */
    public static String getNextHH(Date date,Integer n) {
    	if(n==null) {
    		n = 0;	
    	}
    	Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR,
				calendar.get(Calendar.HOUR) + n);// 让日期加1
		//System.out.println(calendar.get(Calendar.DATE));// 加1之后的日期Top
		//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(calendar.getTime()));
		SimpleDateFormat f = new SimpleDateFormat("HH");
	    String hh = f.format(calendar.getTime());
		return hh;
    }
    
    


    public static void main(String[] args) {
       // String dateStr = "2019-10-01T11:12:34.000Z";
       // System.out.println(new Date().getHours());
       // System.out.println("2019-10-01 11:12:34.00".substring(5,10));
       // System.out.println(DateTimeUtil.differentDays(DateTimeUtil.getDate(dateStr,"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),new Date()));
    	String hh = getNextHH(new Date(),1);
    	System.out.println(hh);
    	System.out.println(getPastDateRfc(7));
    	
    }
}
